import java.util.HashMap;

public class RomanToInt {

    public int romanToInt(String s) {
        int[] val = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
        String[] op = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
        HashMap<String, Integer> map = new HashMap<>();
        for (int i = 0; i < 13; i++) {
            map.put(op[i], val[i]);
        }
        int ans = 0;
        for (int i = 0; i < s.length(); i++) {
            if (i != s.length() - 1 && map.containsKey(s.charAt(i) + "" + s.charAt(i + 1))) {
                ans += map.get(s.charAt(i) + "" + s.charAt(i + 1));
                ++i;
            } else {
                ans += map.get(s.charAt(i) + "");
            }
        }
        return ans;
    }
}
